package com.xiaoshuidi.cloud.module.iot.controller.admin.callback;

import com.xiaoshuidi.cloud.framework.tenant.core.aop.TenantIgnore;
import com.xiaoshuidi.cloud.module.iot.dal.model.callback.YinTongWuLianCallReqVO;
import com.xiaoshuidi.cloud.module.iot.enums.ApiConstants;
import com.xiaoshuidi.cloud.module.iot.enums.YinTongDataTypeEnum;
import com.xiaoshuidi.cloud.module.iot.service.YinTongWuLiaCallbackService;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 银通物联回调接口
 * note:银通物联不接受任何响应内容，数据推送异常也不再推送；故我们仅能根据响应的内容做一些幂等处理
 *
 * @author wael
 * @since 2022.11.7
 */
@RestController
@Tag(name = "智能设备 - 银通物联回调")
@RequestMapping(ApiConstants.NAME + "/callback")
@Slf4j
public class YinTongWuLianDeviceController {
    @Autowired
    private YinTongWuLiaCallbackService yinTongWuLiaCallbackService;

    /**
     * 银通物联推送数据的接口
     */
    @PostMapping("/yintong")
    @TenantIgnore
    public void disposeCallBack(@RequestBody YinTongWuLianCallReqVO vo) {
        log.info("银通物联推送消息__操作id:{}　请求参数：{}", vo.getOperationId(), vo);
        if (StringUtils.isEmpty(vo)) {
            log.error("银通未推送任何内容返回");
            return;
        }
        String dataType = vo.getDataType();
        if (null == YinTongDataTypeEnum.getInstanceByDataTypeCode(dataType)) {
            log.error("银通推送未定义的消息类型 {},无法处理", dataType);
            return;
        }
        yinTongWuLiaCallbackService.dealData(vo);
        log.info("银通物联推送消息处理完毕 {}", vo);
    }
}
